#ifndef IMAGEPREVIEWAREA_H
#define IMAGEPREVIEWAREA_H

#include "FileInfo.h"
#include <QFuture>
#include <QFutureWatcher>
#include <QLabel>
#include <QMediaPlayer>
#include <QPixmap>
#include <QPushButton>
#include <QResizeEvent>
#include <QTimer>
#include <QVector>
#include <QVideoSink>
#include <QWidget>

class ClickableLabel : public QLabel
{
    Q_OBJECT

public:
    explicit ClickableLabel(QWidget *parent = nullptr, Qt::WindowFlags f = Qt::WindowFlags());
    ~ClickableLabel();

signals:
    void clicked();

protected:
    void mouseReleaseEvent(QMouseEvent *event) override;
};

class ImagePreviewArea : public QWidget
{
    Q_OBJECT

public:
    explicit ImagePreviewArea(QWidget *parent = nullptr);
    ~ImagePreviewArea();
    void setImageList(const QVector<FileInfo> &imageList);

signals:
    void imageSelected(const FileInfo &fileInfo);

protected:
    void resizeEvent(QResizeEvent *event) override;

private:
    void setupUI();
    void updatePreview();
    void updateImageSizes();
    void exportImages();

    QVector<ClickableLabel *> m_imageLabels;
    QVector<ClickableLabel *> m_nameLabels;
    QPushButton *m_prevButton;
    QPushButton *m_nextButton;
    QLabel *m_pageInfoLabel;
    QPushButton *m_exportButton;

    QVector<FileInfo> m_imageList;
    int m_currentPage;
    int m_imagesPerPage;
    int m_imageSize;

    QMediaPlayer m_mediaPlayer;
    QVideoSink m_videoSink;

    QHash<QString, QPixmap> m_pixmapCache; // 添加缓存成员

    QFutureWatcher<void> *m_futureWatcher;
    bool m_isLoading;

    void loadImageAsync(int imageIndex, int labelIndex);

    QVector<QFuture<void>> m_runningTasks; // 存储正在运行的异步任务

private slots:
    void onPrevClicked();
    void onNextClicked();
    void onImageClicked(int index);
};

#endif // IMAGEPREVIEWAREA_H
